package cn.pengpeng.day02;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 流数据读取，和写入文件
 * @author root
 *
 */
public class StreamTest {
	FileSystem fs = null;
	@Before
	public void init() throws IOException{
		System.setProperty("HADOOP_USER_NAME", "root");
		//加载配置信息  能够加载core-site.xml   hdfs-site.xml mapred-site.xml yarn...   
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://hadoop01:9000");
		fs = FileSystem.get(conf);
	}
	
	/**
	 * 流文件写入
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void writeFile() throws IllegalArgumentException, IOException{
		FSDataOutputStream output = fs.create(new Path("/writeFile.txt"));
		output.writeDouble(1.1);
		output.writeUTF("hello");
		output.write("hello".getBytes());
		output.close();
	}
	
	
	/**
	 * 流文件读取
	 * @throws IllegalArgumentException 
	 * @throws IOException 
	 */
	@Test
	public void readFile() throws IllegalArgumentException, IOException{
		FSDataInputStream inputStream = fs.open(new Path("/writeFile.txt"));
		double readDouble = inputStream.readDouble();
		String readUTF = inputStream.readUTF();
		System.out.println(readDouble);
		System.out.println(readUTF);
		inputStream.close();
	}
	
	
	
	/**
	 * 作业：
	 * 1：使用流文件的形式  把本地的文件上传到hdfs上 
	 * 2：从hdfs上使用流读取文件写入到本地
	 * @throws IOException
	 */
	
	
	/**
	 * 怎么造成的乱码？
	 * 1：源头的编码是什么样的（gbk）
	 * 2：
	 * 3：读的时候按照utf-8读的
	 * 
	 * 怎么解决？
	 * 1：把源文件修改为utf-8
	 * 2：读的时候指定编码格式
	 * 
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	@Test
	public void readFile2() throws IllegalArgumentException, IOException{
		
		FSDataInputStream inputStream = fs.open(new Path("/hello.txt"));
		//向按照行来读
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"gbk"));
		String line = null;
		while((line=reader.readLine())!=null){
			
			System.out.println(line);
		}
		reader.close();
		inputStream.close();
	}
	
	/**
	 * 下载
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	@Test
	public void readFile3() throws IllegalArgumentException, IOException{
		FSDataInputStream inputStream = fs.open(new Path("/hello.txt"));
		
		FileOutputStream outputStream = new FileOutputStream("d:/hello.txt");
		
		byte[] b = new byte[4096];
		int length = 0;
		while((length = inputStream.read(b))!=-1){
			outputStream.write(b, 0, length);
		}
		outputStream.close();
		inputStream.close();
	}
	
	@After
	public void after() throws IOException{
		fs.close();
	}
}
